package blog.controller;

import blog.common.Result;
import blog.entity.User;

import blog.service.UserService;
import blog.utils.EmailUtil;
import blog.utils.JwtUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 登录
     */
    @PostMapping("/login")
    public Result Login(@RequestBody User user){
        if (StringUtils.isEmpty(user.getUsername()) || StringUtils.isEmpty(user.getPassword())){
            return Result.error("用户名或密码不能为空");
        }
        User user1 = userService.getOne(new LambdaQueryWrapper<User>().eq(User::getUsername, user.getUsername()));
        if (user1 == null){
            return Result.error("用户名不存在");
        }
        if (!user1.getPassword().equals(user.getPassword())){
            return Result.error("密码错误");
        }
        if (user1.getStatus() == 0){
            return Result.error("该用户已被禁用");
        }
        Map<String, Object> map = new HashMap<>();
        map.put("token", JwtUtil.getToken(Long.valueOf(user1.getId())));
        user1.setPassword(null);
        map.put("user", user1);
        return Result.success(map,"登录成功");


    }
    /**
     * 注册
     * @param user
     * @return
     */
    @PostMapping
    public Result register(@RequestBody User user){
        if (StringUtils.isEmpty(user.getUsername()) || StringUtils.isEmpty(user.getPassword())){
            return Result.error("用户名或密码不能为空");
        }
        //判断用户名是否存在
        User user1 = userService.getOne(new LambdaQueryWrapper<User>().eq(User::getUsername, user.getUsername()));
        if (user1 != null){
            return Result.error("用户名已存在");
        }
        //判断邮箱是否存在
        User user2 = userService.getOne(new LambdaQueryWrapper<User>().eq(User::getEmail, user.getEmail()));
        if (user2 != null){
            return Result.error("邮箱已存在");
        }
        //判断邮箱是否合法
        if (!EmailUtil.checkEmail(user.getEmail())){
            return Result.error("邮箱不合法");
        }
        //判断密码是否合法
        if (user.getPassword().length() < 3){
            return Result.error("密码长度不能小于3位");
        }
        //判断验证码是否正确
//        if (!user.getCode().equals("123456")){
//            return Result.error("验证码错误");
//        }
        //保存用户
        userService.save(user);
        return Result.success("注册成功");
    }
    /*
    *修改用户信息
    *http://localhost:8080/user/2
     */
    @PutMapping("/{id}")
    public Result update(@PathVariable("id") Integer id, @RequestBody User user){
        user.setId(id);
        //判断要修改的用户是否存在
        User user1 = userService.getById(id);
        if (user1 == null){
            return Result.error("用户不存在");
        }
        //判断用户名是否存在
        User user2 = userService.getOne(new LambdaQueryWrapper<User>().eq(User::getUsername, user.getUsername()));
        if (user2 != null && !user2.getId().equals(id)){
            return Result.error("用户名已存在");
        }
        boolean b = userService.updateById(user);
        if (b){
            return Result.success(user,"修改成功" );
        }
        return Result.error("修改失败");
    }


    /*
    根据id查询用户信息
     */
    @GetMapping("/{id}")
    public Result getUserById(@PathVariable("id") Integer id){
        User user = userService.getById(id);
        if (user == null){
            return Result.error("用户不存在");
        }
        return Result.success(user);
    }
}